import { platformCore, Module, APP_INITIALIZER, Injector, Injectable } from '@notadd/core';
import {
    IncomingRequestMessage,
    LoggerFactory,

} from '@gb28181/core';
import { Server } from 'ws';
import { SipParser } from './sip/sip-parser';
import { UserAgent, Session, SessionDescriptionHandler, SessionDescriptionHandlerOptions, SessionDescriptionHandlerModifier, BodyAndContentType, Registerer } from '@gb28181/sip';
export class SessionDescriptionHandlerImpl implements SessionDescriptionHandler {
    _sdp: string;
    constructor(private session: Session, private options: SessionDescriptionHandlerOptions) {

    }
    close(): void {
        console.log(`SessionDescriptionHandler close`)
    }
    async getDescription(options?: SessionDescriptionHandlerOptions | undefined, modifiers?: SessionDescriptionHandlerModifier[] | undefined): Promise<BodyAndContentType> {
        return {
            body: this._sdp,
            contentType: 'application/sdp'
        }
    }
    hasDescription(contentType: string): boolean {
        return true;
    }
    async setDescription(sdp: string, options?: SessionDescriptionHandlerOptions | undefined, modifiers?: SessionDescriptionHandlerModifier[] | undefined): Promise<void> {
        this._sdp = sdp;
    }
    sendDtmf(tones: string, options?: unknown): boolean {
        throw new Error("Method not implemented.");
    }
}
import { Transport } from '@gb28181/sip-node'
@Module({
    providers: [
        SipParser,
        {
            provide: APP_INITIALIZER,
            useValue: (injector: Injector) => {
                const server = new Server({
                    host: '0.0.0.0',
                    port: 3000
                });
                const allUas = new Map()
                server.on('connection', (socket) => {
                    socket.binaryType = 'arraybuffer';
                    const uas = new UserAgent({
                        uri: UserAgent.makeURI(`sip:echo@sipjs.onsip.com`),
                        transportConstructor: Transport,
                        sessionDescriptionHandlerFactory: (session: Session, options?: any) => {
                            return new SessionDescriptionHandlerImpl(session, options);
                        },
                        sessionDescriptionHandlerFactoryOptions: {},
                        transportOptions: {
                            server: socket
                        },
                        delegate: {
                            onRegister: (register: Registerer) => { 
                                console.log(register)
                            },
                            onInvite: (invite) => {
                                // 找到另一个人
                                const message = invite.request
                                const { from, fromTag, to, toTag } = message;
                                invite.accept()
                            },
                            onMessage: (invite) => {

                            },
                            onConnect: () => {
                                console.log(`onConnect`)
                            }
                        }
                    });;
                    uas.start().then(() => {
                        console.log(`uas start success`)
                    }).catch(e => {
                        console.log(e)
                    })
                });
                server.on('listening', () => {
                    console.log(`sip server listen at ws://localhost:3000`)
                });
            },
            multi: true
        }
    ]
})
export class AppModule { }
platformCore().bootstrapModule(AppModule);